<template>
  <el-dialog
    title="隐患分类编辑"
    :visible.sync="show"
    width="700px"
    center
  >
    <div class="btns">
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='same'">同级新增</el-button>
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='under'">下级新增</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="modify">修改</el-button>
    </div>
    <div class="dirList">
      <tree-box v-model="selectedNode" :data="treeData" :no-search="true" title="目录" />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="show = false">确 定</el-button>
    </span>
    <el-dialog
      title="安全隐患分类信息"
      :visible.sync="showAdd"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="90px">
        <el-form-item label="分类编号" prop="number">
          <el-input v-model="form.number" :disabled="disableForm" />
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" :disabled="disableForm" />
        </el-form-item>
        <el-form-item label="是否启用" prop="enable">
          <el-checkbox v-model="form.enable" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="form.description" :disabled="disableForm" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="saveClassify">确 定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { addSecurityRiskCategory, updateSecurityRiskCategory } from '@/api/securityManage'

export default {
  name: 'DirManage',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        name: '',
        number: '',
        description: '',
        enable: true
      },
      rules: {
        name: [
          { required: true, message: '请输入安全隐患名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showAdd(newValue, oldValue) {
      if (!newValue) {
        this.$refs.form.resetFields()
      }
    }
  },
  methods: {
    modify() {
      this.showAdd = true
      this.addType = ''
      const { data } = this.selectedNode || {}
      if (data) {
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            this.form[key] = data[key]
          })
        })
      }
    },
    saveClassify() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const { id, parentId } = this.selectedNode
          const createId = this.addType === 'same' ? (parentId || null) : id
          const params = {
            ...this.form,
            parentId: this.addType ? createId : parentId,
            ...!this.addType ? { id } : {}
          }
          const fn = this.addType ? addSecurityRiskCategory : updateSecurityRiskCategory
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.showAdd = false
              this.$emit('freshTree')
              // this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dirList{
  max-height: 500px;
  min-height: 200px;
}

</style>
